package com.example.question.list.list1;

/**
 * @Date 2023-07-04
 * @Author Jonathan
 * @link {<a href="https://leetcode.cn/problems/middle-of-the-linked-list/">...</a>}
 */
public class Code06_876 {
    public static void main(String[] args) {
        Code06_876 code = new Code06_876();
        // 0,1, 2, 3, 4, 5
        // 0,1, 2, 3, 4
        ListNode list1 = List.createList(new int[]{1, 2, 3, 4, 5});
        ListNode list2 = List.createList(new int[]{1, 2, 3, 4, 5, 6});
        System.out.println(code.middleNode(list1));
        System.out.println(code.middleNode(list2));
    }


    public ListNode middleNode(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        // 当fast == null 结束循环的时候 节点个数为偶数 此时slow指向到下中点
        // 当fast.next == null 时 节点个数为奇数  slow 指向中点
        return slow;
    }
}
